package com.atguigu.wirte;

import com.atguigu.bean.Movie;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Bulk;
import io.searchbox.core.Index;

import java.io.IOException;

public class Bulk2_Write {
    public static void main(String[] args) throws IOException {
        //1.创建客户端工厂
        JestClientFactory jestClientFactory = new JestClientFactory();

        //设置ES连接地址
        HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://hadoop102:9200").build();

        jestClientFactory.setHttpClientConfig(httpClientConfig);

        //2.获取连接
        JestClient jestClient = jestClientFactory.getObject();

        //3.使用连接，往ES中写入数据
        Movie movie6 = new Movie("106", "哈利波特");
        Movie movie7 = new Movie("107", "速度与激情");
        Movie movie8 = new Movie("108", "第一滴血");

        Index index6 = new Index.Builder(movie6).id("1006").build();
        Index index7 = new Index.Builder(movie7).id("1007").build();
        Index index8 = new Index.Builder(movie8).id("1008").build();


        Bulk bulk = new Bulk.Builder()
                .addAction(index6)
                .addAction(index7)
                .addAction(index8)
                .defaultIndex("movie1")
                .defaultType("_doc")
                .build();

        jestClient.execute(bulk);

        //关闭连接
        jestClient.shutdownClient();
    }
}
